package academic.ui.client.view;


import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import academic.ui.client.presenter.CheckoutPresenter;
import academic.ui.client.rules.Rules;
import academic.ui.client.widgets.SignInDiv;
import academic.ui.client.widgets.YourPurchase;
import academic.ui.client.widgets.forms.AddEditCCForm;
import academic.ui.client.widgets.forms.RegistrationForm;
import ecommerce.soco.shared.GWT_CreditCard;
import ecommerce.soco.shared.GWT_Customer;
import ecommerce.soco.shared.GWT_Gift;
import ecommerce.soco.shared.GWT_Product;
import ecommerce.soco.shared.GWT_Zone;

public class CheckoutView extends ViewImpl implements CheckoutPresenter.MyView {

	private GenericContainerTag checkoutPanel = new GenericContainerTag("div");
	

	// Used for hiding and showing payment options
	private GenericContainerTag cardDiv = new GenericContainerTag("div");

	private Button confirmOrder = new Button();
	
	private RegistrationForm registrationForm = new RegistrationForm();

	private AddEditCCForm checkoutForm = new AddEditCCForm();
	
	private GenericContainerTag registrationDiv = new GenericContainerTag("div");
	private SignInDiv signInDiv = new SignInDiv();
	private YourPurchase purchasePanel = new YourPurchase();
	
	private GenericContainerTag purchaseContentBox = new GenericContainerTag("div");
	private GenericContainerTag checkoutContentBox = new GenericContainerTag("div");
	private GenericContainerTag mainContentBox = new GenericContainerTag("div");
	private GenericContainerTag gameSlot = new GenericContainerTag("div");


	private GenericClickable buyAsGift = new GenericClickable("a","buy as a gift");
	private Hidden emailMsg = new Hidden("message");
	private Hidden recipientEmail = new Hidden("email");
	private Hidden hasGift = new Hidden("hasGift");
	private Hidden sendDate = new Hidden("senddate");
	
	private GenericClickable createEvent = new GenericClickable("a","create an event");

	@Inject
	public CheckoutView() {
		gameSlot.setStyleName("gamePresenterSlot");
		gameSlot.add(mainContentBox);
		mainContentBox.setStyleName("outerContent");
		mainContentBox.add(purchaseContentBox);
		mainContentBox.add(checkoutContentBox);
		purchaseContentBox.setStyleName("checkoutContent");
		
		
		GenericContainerTag yourPurchaseDiv = new GenericContainerTag("div");
		purchaseContentBox.add(yourPurchaseDiv);
		yourPurchaseDiv.add(purchasePanel);
		yourPurchaseDiv.add(buyAsGift);
		yourPurchaseDiv.add(hasGift);
		yourPurchaseDiv.add(recipientEmail);
		yourPurchaseDiv.add(emailMsg);
		yourPurchaseDiv.add(sendDate);
		yourPurchaseDiv.add(createEvent);
		yourPurchaseDiv.setStyleName("yourPurchaseDiv");
		
		buyAsGift.getElement().setId(CheckoutPresenter.SELECTOR);
		buyAsGift.getElement().setAttribute("rel", "#giftModal");
		
		
		createEvent.getElement().setId(CheckoutPresenter.EVENT_SELECTOR);
		createEvent.getElement().setAttribute("rel", "#eventModal");
		
		checkoutContentBox.setStyleName("checkoutContent");
		checkoutContentBox.add(checkoutPanel);
		cardDiv.setVisible(true);
		cardDiv.setStyleName("cardDivCheckout");
		registrationForm.setStyleName("checkoutRegistration");
		checkoutForm.setStyleName("checkoutBill");
		GenericContainerTag confirmDiv = new GenericContainerTag("div");
		confirmDiv.setStyleName("confirmDiv");
		confirmDiv.add(confirmOrder);
		confirmOrder.setStyleName("confirmOrder");
		DOM.setStyleAttribute(confirmOrder.getElement(), "width", "208px");
		// personal info
		checkoutPanel.setStyleName("checkoutBody");
		registrationDiv.clear();
		registrationDiv.setStyleName("registrationDiv");
		checkoutPanel.add(registrationDiv);
		
		GenericContainerTag float50 = new GenericContainerTag("div");
		float50.setStyleName("float50");
		registrationDiv.add(float50);
		float50.add(registrationForm);
		
		GenericContainerTag float501 = new GenericContainerTag("div");
		float501.setStyleName("float50");
		
		registrationDiv.add(float501);
		float501.add(signInDiv);
		
		GenericContainerTag billHeader = new GenericContainerTag("div");
		GenericContainerTag h3 = new GenericContainerTag("h3","Billing Information");
		billHeader.add(h3);
		billHeader.setStyleName("billingHeader");
		checkoutPanel.add(billHeader);
		checkoutPanel.add(checkoutForm);

		checkoutPanel.add(cardDiv);
	
		checkoutPanel.add(confirmDiv);
			
	}

	@Override
	public Widget asWidget() {
		return gameSlot;
	}

	@Override
	public HasClickHandlers confirmOrder() {
		return confirmOrder;
	}


	@Override
	public void addZoneOptions(GWT_Zone[] zones) {
		checkoutForm.setStateOptions(zones);
		//addEditCCForm.setStateOptions(zones);
	}
	

	@Override
	public void setLoginError(String string) {
		// TODO Auto-generated method stub

	}
	
	@Override
	public Integer cardID() {
		return cardID;
	}
	private Integer cardID=-1;

	

	@Override
	public GWT_Customer getRegisterCustomer() {
		return registrationForm.getCustomer();
	}


	@Override
	public GWT_CreditCard getCard() {
		int getRadioId = getRadioId();
		if(getRadioId>0)
		{
			GWT_CreditCard card = new GWT_CreditCard();
			card.setCreditCardId(getRadioId);
			return card;
		}
		else
			return checkoutForm.getCard(cardID);		
	}


	@Override
	public void setupCheckoutRegistration() {
		registrationForm.setVisible(true);
		checkoutForm.setVisible(true);
		cardDiv.setVisible(false);
		confirmOrder.setVisible(true);
		registrationDiv.setVisible(true);
	}


	@Override
	public void setupLoggedInNoCards() {
		checkoutForm.setVisible(true);
		registrationDiv.setVisible(false);
	}
	
	@Override
	public void setupLoggedInWithCards() {
		registrationDiv.setVisible(false);
		checkoutForm.setVisible(false);
		cardDiv.setVisible(true);
	}


	/**
	 * return false if there are problems
	 */
	@Override
	public boolean checkCheckoutRegistrationRules() {
		Boolean checkRules = true;
		if(registrationDiv.isVisible())
			checkRules = registrationForm.checkRules();
		if(checkoutForm.isVisible())
			checkRules = checkRules && checkoutForm.checkRules();
		return checkRules;
	}

	@Override
	public void showPurchase(GWT_Product result) {
		purchasePanel.setup(result);
	}
	
	@Override
	public HasClickHandlers getSignInLink() {
		return signInDiv.getSignInLink();
	}

	@Override
	public Integer getQuantity() {
		return purchasePanel.getQuantity();
	}

	@Override
	public void throwError(int lOGINERRORUSERNOTFOUND) {
		if(lOGINERRORUSERNOTFOUND==Rules.LOGIN_ERROR_USER_NOT_FOUND)
			registrationForm.setError("User email/password combination not found.");
	}

	/**
	 * Use this method to determine if one of the already validated credit cards is selected.
	 */
	@Override
	public native int getRadioId() /*-{
		var radio = $wnd.$('.myCardsDiv input:radio[name=paymentOption]:checked');
		if(radio)
			return radio.parent().attr('id');
	}-*/;

	@Override
	public void addContent(Object slot, Widget w) {
		if(slot==CheckoutPresenter.slot)
		{
			cardDiv.clear();
			cardDiv.add(w);
		}
	}

	@Override
	public HasClickHandlers getBuyAsGiftLink() {
		return buyAsGift;
	}
	
	@Override
	public HasClickHandlers createEventLink() {
		return createEvent;
	}
	
	@Override
	public GWT_Gift getGift() {
		if(hasGift.getValue().equals("true")) 
		{
			GWT_Gift gift = new GWT_Gift();
			gift.setEmailMsg(emailMsg.getValue());
			gift.setRecipientEmail(recipientEmail.getValue());
			gift.setSendDate(sendDate.getValue());
			return gift;
		}
		else
			return null;
	}
	

	
}
